namespace EntityFrameworkSample.Migrations
{
    using EntityFrameworkSample.DB.Model;
    using System;
    using System.Data.Entity.Migrations;
    using System.Text;

    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Blog",
                c => new
                    {
                        BlogId = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                    })
                .PrimaryKey(t => t.BlogId);
            
            CreateTable(
                "dbo.ContentType",
                c => new
                    {
                        ID = c.Int(nullable: false),
                        Name = c.String(nullable: false, maxLength: 50),
                    })
                .PrimaryKey(t => t.ID);

            Sql("INSERT INTO dbo.Blog (Name) VALUES ('test')");

            Sql("if TYPE_ID(N'IntIdTableType') IS NULL CREATE TYPE dbo.IntIdTableType AS TABLE ( Id int)");
            Sql("if TYPE_ID(N'StrIdTableType') IS NULL CREATE TYPE dbo.StrIdTableType AS TABLE ( Id nvarchar(200))");

            var insertAssetTypeSqlBuilder = new StringBuilder();
            Enum.GetNames(typeof(ContentTypeEnum)).Each(typeName =>
            {
                insertAssetTypeSqlBuilder.AppendFormat("INSERT INTO dbo.ContentType (ID, Name) VALUES ({0}, '{1}')",
                    (int)Enum.Parse(typeof(ContentTypeEnum), typeName),
                    typeName);
                insertAssetTypeSqlBuilder.AppendLine();
            });
            Sql(insertAssetTypeSqlBuilder.ToString());
        }
        
        public override void Down()
        {
            DropTable("dbo.ContentType");
            DropTable("dbo.Blog");
            Sql("if TYPE_ID(N'IntIdTableType') IS NOT NULL DROP TYPE dbo.IntIdTableType");
            Sql("if TYPE_ID(N'StrIdTableType') IS NOT NULL DROP TYPE dbo.StrIdTableType");
        }
    }
}
